﻿#pragma once
#include <unordered_set>
#include "MdbStructs.h"
#include "MdbPrimaryKeyComp.h"

namespace mdb
{
	class TickTable;
	class TickPrimaryKey
	{
		using iterator = std::unordered_set<Tick*, TickHashForTickPrimaryKey, TickEqualForTickPrimaryKey>::iterator;
		friend class TickTable;
	public:
		TickPrimaryKey(TickTable* table, size_t buckets = 1000);
		Tick* Select(const Int64Type& TradingDay, const ExchangeIDType& ExchangeID, const InstrumentIDType& InstrumentID, const Int64Type& UpdateTs);
		std::pair<iterator, iterator> SelectAll();
		
	protected:
		bool Insert(Tick* const record);
		void Erase(Tick* const record);
		bool CheckInsert(Tick* const record);
		bool CheckUpdate(const Tick* const oldRecord, const Tick* const newRecord);

	private:
		TickTable* m_Table;
		std::unordered_set<Tick*, TickHashForTickPrimaryKey, TickEqualForTickPrimaryKey> m_Index;
	};

	class BarTable;
	class BarPrimaryKey
	{
		using iterator = std::unordered_set<Bar*, BarHashForBarPrimaryKey, BarEqualForBarPrimaryKey>::iterator;
		friend class BarTable;
	public:
		BarPrimaryKey(BarTable* table, size_t buckets = 1000);
		Bar* Select(const Int64Type& TradingDay, const ExchangeIDType& ExchangeID, const InstrumentIDType& InstrumentID, const Int64Type& UpdateTs);
		std::pair<iterator, iterator> SelectAll();
		
	protected:
		bool Insert(Bar* const record);
		void Erase(Bar* const record);
		bool CheckInsert(Bar* const record);
		bool CheckUpdate(const Bar* const oldRecord, const Bar* const newRecord);

	private:
		BarTable* m_Table;
		std::unordered_set<Bar*, BarHashForBarPrimaryKey, BarEqualForBarPrimaryKey> m_Index;
	};

}
